import commentListData from 't/mock/mock-data/comment/CMTS-GetCommentsByPageID.js';
import {
  ATTACHMENT_MAX_SIZE_KEY,
  RECENTLY_MENTIONED_USERS_KEY,
} from '../utils/constants';
import { isUseMock } from '../Component';
// 开发用Mock数据
import userProfileData from 't/mock/mock-data/comment/WRP-Runtime-GetUserProfile.js';
import commentReliesData from 't/mock/mock-data/comment/CMTS-GetRepliesList.js';
import userData from 't/mock/mock-data/comment/SYS_UserMgr_GetSysUserById.js';

export const getTFF = () => {
  return (
    window.TFF || {
      common: {
        ctx: {
          getSession: () => {},
        },
        ajax: {
          getDataAsync: () => {},
          getServiceUrl: () => {},
        },
      },
    }
  );
};

const { ajax, ctx } = getTFF().common;
const { getServiceUrl, getDataAsync } = ajax;

/** 获取用户个性化设置 */
export const getUserProfiles = async _ => {
  if (isUseMock.value || !window.TFF) return userProfileData;
  return getDataAsync({
    url: getServiceUrl('WRP-Runtime-GetUserProfile'),
    type: 'post',
    data: {
      profileKey: [ATTACHMENT_MAX_SIZE_KEY, RECENTLY_MENTIONED_USERS_KEY].join(
        ',',
      ),
    },
  });
};
/** 保存用户个性化设置 */
export const saveUserStringProfile = async users => {
  if (isUseMock.value || !window.TFF) return userProfileData;
  try {
    return getDataAsync({
      url: getServiceUrl('WRP-Runtime-SaveUserStringProfile'),
      method: 'POST',
      data: {
        profileKey: RECENTLY_MENTIONED_USERS_KEY,
        profileValue: JSON.stringify(users),
      },
    });
  } catch (error) {
    console.log(error);
  }
};

export const getSysLoginUser = async _ => {
  if (isUseMock.value || !window.TFF) return {
    Avatar: 'https://dummyimage.com/32',
    UserID: '1234567890',
    RealName: 'DemoUserName',
  };
  return getDataAsync({
    url: getServiceUrl('SYS_UserMgr_GetLoginSysUser'),
  });
};

/** 获取登录用户信息 */
export const getUserInfoFromSession = async _ => {
  if (isUseMock.value || !window.TFF) {
    return {
      CurUserForWebUI: {
        Avatar: 'https://dummyimage.com/32',
        UserId: '1234567890',
        UserName: 'DemoUserName',
      },
      CustInfomationVMSG: {
        headImg: 'https://dummyimage.com/32',
      },
    };
  }

  return ctx.getSession();
};

/** 获取评论列表 */
export const getCommentList = async params => {
  // return { state:'1',data: { Items: [], TotalCount: 0, Properties: { UICount :0} } };
  if (isUseMock.value || !window.TFF) return commentListData;
  // const { getServiceUrl, getDataAsync } = TFF.common.ajax;
  return getDataAsync({
    url: getServiceUrl('CMTS-GetCommentsByPageID'),
    data: { filter: JSON.stringify(params) },
  });
};

/** 发布评论 */
export const saveComment = payload => {
  if (isUseMock.value || !window.TFF) {
    return { state: '1', data: payload };
  }
  return getDataAsync({
    url: getServiceUrl('CMTS-SaveComment'),
    method: 'POST',
    data: { comment: JSON.stringify(payload) },
  });
};

/** 删除评论 */
export const deleteComment = async id => {
  if (isUseMock.value || !window.TFF) return { state: '1' };
  return getDataAsync({
    url: getServiceUrl('CMTS-CancelComment'),
    method: 'POST',
    data: { id },
  });
};

/** 获取回复列表 */
export const GetRepliesList = async parmas => {
  if (isUseMock.value || !window.TFF) return commentReliesData;
  // const { getServiceUrl, getDataAsync } = TFF.common.ajax;
  return getDataAsync({
    url: getServiceUrl('CMTS-GetRepliesList'),
    method: 'POST',
    data: { input: JSON.stringify(parmas) },
  });
};

/** 发布回复 */
export const saveReply = async payload => {
  if (isUseMock.value || !window.TFF) return { state: '1', data: payload };
  return getDataAsync({
    url: getServiceUrl('CMTS-SaveReply'),
    method: 'POST',
    data: { reply: JSON.stringify(payload) },
  });
};

/** 删除回复 */
export const deleteReply = async id => {
  if (isUseMock.value || !window.TFF) return {state: '1'};
  return getDataAsync({
    url: getServiceUrl('CMTS-CancelReply'),
    method: 'POST',
    data: { id },
  });
};

/** 点亮红心 */
export const turnOnLike = async payload => {
  if (isUseMock.value || !window.TFF) return { state: '1' };
  return getDataAsync({
    url: getServiceUrl('CMTS-Like'),
    method: 'POST',
    data: { comment: JSON.stringify(payload) },
  });
};

/** 取消红心 */
export const cancelLike = async payload => {
  if (isUseMock.value || !window.TFF) return { state: '1' };
  return getDataAsync({
    url: getServiceUrl('CMTS-CancelLike'),
    method: 'POST',
    data: { input: JSON.stringify(payload) },
  });
};

/** 获取公共接口的人员信息列表 */
export const getSysCommonUserList = async payload => {
  if (isUseMock.value || !window.TFF) {
    const list = JSON.parse(
      userProfileData.data.wrp_recently_mentioned_users,
    ).filter(
      item =>
        item.RealName.includes(payload.FieldValue) ||
        item.Account.includes(payload.FieldValue) ||
        item.Email.includes(payload.FieldValue) ||
        item.Mobile.includes(payload.FieldValue),
    );
    return {
      rows: list,
      total: list.length,
    };
  }

  return getDataAsync({
    url: getServiceUrl('SYS_CommonHelper_GetUserList&help=true'),
    method: 'POST',
    data: { filter: JSON.stringify(payload) },
  });
};

/** 获取公共接口的人员信息 */
export const getSysUserInfoById = async userId => {
  if (isUseMock.value || !window.TFF) return userData;

  return getDataAsync({
    url: getServiceUrl('SYS_UserMgr_GetSysUserById'),
    method: 'POST',
    data: { userId: JSON.stringify(userId) },
  });
};

/**
 * 文件上传
 *  - 在编辑框粘贴图片时【不可使用 base64 图片，内容太大】
 *    会先调用上传，再用得到的图片地址创建 img 标签并插入编辑框，
 *  - 直接以附件形式上传文件
 */
export const apiUploadFile = async ({ fileName, fileContent }) => {
  if (isUseMock.value || !window.TFF) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          state: '1',
          data: {
            ID: Date.now().toString(),
            AttachmentUrl:
              'http://resource.teld.cc/teldimage/160/20240524151015image.png',
            FileName: 'test_upload_file_image_20240226154537_file-name-very-long.png',
          },
        });
      }, 1500);
    });
  }

  return getDataAsync({
    url: getServiceUrl('CMTS-UploadAttachment'),
    data: {
      param: JSON.stringify({
        FileName: fileName,
        FileContent: fileContent,
      }),
    },
  });
};
